package com.googlecode.antilopebpm.util;

import java.io.InputStream;

import com.googlecode.antilopebpm.exception.AntilopeInternalException;

public final class ConfigHelper {

	/**
	 * @param resource
	 * @return
	 * @throws AntilopeInternalException
	 *             Be Care this Exception
	 */
	public static InputStream getResourceAsStream(String resource)
			throws AntilopeInternalException {
		String stripped = resource.startsWith("/") ? resource.substring(1)
				: resource;

		InputStream stream = null;
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader != null) {
			stream = classLoader.getResourceAsStream(stripped);
		}
		if (stream == null) {
			stream = ConfigHelper.class.getResourceAsStream(resource);
		}
		if (stream == null) {
			stream = ConfigHelper.class.getClassLoader().getResourceAsStream(
					stripped);
		}
		if (stream == null) {
			throw new AntilopeInternalException(resource + " not found");
		}
		return stream;
	}

}
